package edu.java.texbooks.scjp.collections.test07.set;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import edu.java.texbooks.scjp.collections.test07.set.student.Student;

public class TestHashSet {
	static Student student1 = new Student("Pesho Ivanov", 56214, 5.34);
	static Student student2 = new Student("Asen Ivanov", 56378, 5.12);

	static Student student3 = new Student("Petq Milcheva", 46214, 3.34);
	static Student student4 = new Student("Asq Ivanova", 67378, 4.12);
	static Student student5 = new Student("Lora Vasileva", 54623, 5.80);

	public static void main(String[] args) {

		// test01();

		// test02();

		// test03();

		test04();
	}

	public static void test01() {

		// hss= hashSetStudents
		Set<Student> hss = new HashSet<Student>();

		System.out.println(hss);
		System.out.println("size: " + hss.size());

		boolean[] boolArray = new boolean[5];
		boolArray[0] = hss.add(student1);
		boolArray[1] = hss.add(student2);
		boolArray[2] = hss.add(student3);
		boolArray[3] = hss.add(student4);

		// it will be false, because student4 is already added
		boolArray[4] = hss.add(student4);

		System.out.println(Arrays.toString(boolArray));
		System.out.println(hss);
	}

	public static void test02() {

		List<Student> listStudents = new ArrayList<Student>();
		listStudents.add(student1);
		listStudents.add(student2);
		listStudents.add(student3);
		listStudents.add(student4);
		listStudents.add(student4);
		listStudents.add(student4);

		Set<Student> setStudents = new HashSet<Student>(listStudents);
		System.out.println("\nsetSize: " + setStudents.size());

		System.out.println("-------- HashSet ----------------");
		System.out.println("\n" + setStudents);

		System.out.println("-------- Array List ----------------");
		System.out.println("\n" + listStudents);
	}

	public static void test03() {
		Set<Student> setStudents = new HashSet<Student>(2);
		System.out.println("\n" + "size set: " + setStudents.size());

		boolean[] boolArray = new boolean[5];
		boolArray[0] = setStudents.add(student1);
		boolArray[1] = setStudents.add(student2);
		boolArray[2] = setStudents.add(student3);
		boolArray[3] = setStudents.add(student4);
		boolArray[4] = setStudents.add(student1);

		System.out.println("\n" + "size set: " + setStudents.size());
		System.out.println(Arrays.toString(boolArray));
		System.out.println("\n---------------- HashSet -----------------");
		System.out.println(setStudents);

	}

	public static void test04() {
		Set<Student> hss = new HashSet<Student>();

		System.out.println("\n" + "size: " + hss.size());
		System.out.println("Is hss empty: " + hss.isEmpty());

		hss.add(student1);
		hss.add(student2);

		System.out.println("\n----- Print HashSet -------------\n");
		System.out.println(hss);

		hss.clear();
		System.out.println("Print cleared hashSet: " + hss);
		System.out.println("Is hss empty: " + hss.isEmpty());

		hss.add(student3);
		System.out.println("\n--------------------\n");
		System.out.println("Adding a student.");
		System.out.println("Is hss empty: " + hss.isEmpty());

		hss.clear();
		System.out.println("Clear hashSet and addAll students from list");
		List<Student> arrayListStudents = new ArrayList<Student>();
		arrayListStudents.add(student1);
		arrayListStudents.add(student2);
		arrayListStudents.add(student3);
		arrayListStudents.add(student4);
		arrayListStudents.add(student2);

		hss.addAll(arrayListStudents);
		System.out.println("\n----- Print HashSet -------------\n");
		System.out.println(hss.toString());

		System.out.println("\n--------------------\n");
		System.out.println("Check if student " + student1.getName()
				+ " is added to the HashSet -->" + hss.contains(student1));

		hss.add(student1);

		System.out.println("Check if student " + student5.getName()
				+ " is added to the HashSet -->" + hss.contains(student5));

		System.out.println("\n----- Print HashSet -------------\n");
		System.out.println(hss.toString());
		
		boolean bool=hss.remove(student1);
		System.out.println("Try to remove student " + student1.getName()
				+ " from the HashSet -->  "  + bool);
		
		System.out.println("\n----- Print HashSet -------------\n");
		System.out.println(hss.toString());
		
		hss.add(student1);
		hss.add(student5);
		
		System.out.println("\n--------------------\n");
		Iterator<Student> iterator= hss.iterator();
		while(iterator.hasNext()){
			System.out.println(iterator.next().getName() + ", ");
			iterator.remove();
		}
		
		System.out.println("\n--------------------\n");
		System.out.println("Is HashSet empty: " + hss.isEmpty());
		System.out.println("size of HashSet: " + hss.size());
		

	}
}
